<template>
  <div class="dialog_content">
    <el-dialog class="m_dialog" :title="dialogTitles[titleIndex] || titleIndex" :visible.sync="dialogVisible" width="800" append-to-body :before-close="cancel" :close-on-click-modal="false">
      <div class="content">
        <custom-form ref="form" :formModel="formModel" :formItemConfig="formItemConfig" :formRules="formRules" label-width="80px" label-position="left" />
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel" class="btn_cancel">取 消</el-button>
        <el-button type="primary" @click="submitFrom()" class="btn_primary">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addConfig, updateConfig } from '@/api/system/config';
import { mapState } from 'vuex';
export default {
  name: 'ConfigForm',
  props: {
    dialogVisible: {
      type: Boolean,
      default: false,
    },
    titleIndex: {
      type: Number | String,
      default: 0,
    },
    formData: {
      type: Object,
      default: () => {},
    },
    dictMap: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      // 提交表单数据
      formModel: {
        configId: undefined,
        configName: undefined,
        configKey: undefined,
        configValue: undefined,
        configType: 'Y',
        remark: undefined,
      },
      // 表单配置项
      formItemConfig: [
        // {
        //   label: '参数编号',
        //   prop: 'configId',
        //   span: 12,
        //   clearable: true,
        // },
        {
          label: '参数名称',
          prop: 'configName',
          span: 12,
          clearable: true,
        },
        {
          label: '参数键名',
          prop: 'configKey',
          span: 12,
          clearable: true,
        },
        {
          label: '参数键值',
          prop: 'configValue',
          span: 12,
          clearable: true,
        },
        {
          label: '系统内置',
          prop: 'configType',
          component: 'BaseRadioGroup',
          span: 12,
          clearable: true,
        },
        {
          label: '备注',
          prop: 'remark',
          span: 12,
          clearable: true,
        },
      ],
      // 表单验证
      formRules: {
        configName: [{ required: true, message: '参数名称不能为空', trigger: 'blur' }],
        configKey: [{ required: true, message: '参数键名不能为空', trigger: 'blur' }],
        configValue: [{ required: true, message: '参数键值不能为空', trigger: 'blur' }],
      },
    };
  },
  computed: {
    ...mapState('dialog', ['dialogTitles']),
  },
  watch: {
    formData: {
      deep: true,
      immediate: true,
      handler() {
        this.formModel = JSON.parse(JSON.stringify(this.formData));
        this.setDictList();
      },
    },
  },
  created() {},
  mounted() {},
  methods: {
    getList() {
      this.$emit('getList');
    },
    cancel() {
      this.$emit('cancel');
      this.$refs["form"].toResetFields();
    },
    // 设置formItemConfig里面的字典列表
    setDictList() {
      if (this.dictMap) {
        // 系统内置
        this.formItemConfig[this.formItemConfig.findIndex(item => item.prop == 'configType')].options = this.dictMap.type.sys_yes_no;
      }
    },
    // 提交
    submitFrom() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.$utils.isNotEmpty(this.formModel.configId)) {
            updateConfig(this.formModel).then(res => {
              if (200 === res.code) {
                this.$message({
                  message: '修改成功',
                  type: 'success',
                });
                this.cancel();
                this.getList();
              }
            });
          } else {
            addConfig(this.formModel).then(res => {
              if (200 === res.code) {
                this.$message({
                  message: '添加成功',
                  type: 'success',
                });
                this.cancel();
                this.getList();
              }
            });
          }
        } else {
        }
      });
    },
  },
  components: {},
};
</script>

<style lang="less" scoped></style>
